#include "Timer.h"
#include <iostream>
#include <SDL.h>

Timer::Timer() : 
    m_dt(0.0f), 
    m_oldTicks(0), 
    m_frameRate(0.0f), 
    m_frames(0.0f),
    m_timeKeep(0.0f),
    m_printing(false)
{}
void Timer::Update()
{
	unsigned int newTicks = SDL_GetTicks(); //milliseconds
	unsigned int diff = newTicks - m_oldTicks;
	m_oldTicks = newTicks;
	m_dt = (float)diff;
    CalculateFrameRate();
}

const float Timer::GetDtSeconds() const 
{
	return m_dt/1000;
}
const float Timer::GetDtMilliSeconds() const
{
    return m_dt;
}
unsigned int Timer::GetElapsedTime()
{
	return m_oldTicks;
}

const float Timer::GetFrameRate()
{   
    return m_frameRate;
}

void Timer::CalculateFrameRate()
{
    m_frames++;
    m_timeKeep += m_dt / 1000;
    if ( m_timeKeep > 1)
    {
        m_frameRate = m_frames;
        m_timeKeep = 0.0f;		
        m_frames = 0.0f;
    }
}

void Timer::TogglePrint()
{
    m_printing = !m_printing;
}